package com.linfnn.demo.Controller;

import com.linfnn.demo.Model.Assessment;
import com.linfnn.demo.Service.impl.AssessmentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Timer;


@Controller
public class FunctionAssessment {

    @Autowired
    private AssessmentServiceImpl assessmentService;

//  这是管理页面，同时可以刷新数据，进行操作后跳到这里会重新获取数据，从而刷新页面
    @RequestMapping(value = "/manage_assessments")
    public ModelAndView showAll() {
        List<Assessment> assessments = assessmentService.list();
        //        返回的页面
        ModelAndView mav = new ModelAndView("/managehtml/assessmentManage.html");
        //        页面和对应的数据结合
        mav.addObject("results", assessments);
        return mav;
    }

//
    @PostMapping("/search/Assessment")
    public ModelAndView search(@RequestParam("assessmentName") String assessmentName) {
        Assessment assessment = assessmentService.getAssessmentByName(assessmentName);
        ModelAndView mav = new ModelAndView("/search/index.html");
        mav.addObject("results", assessment);
        return mav;
    }

//    注意这里返回应该更换为html，不能再去直接刷新获取整个数据库的数据， 并且这里的路由为searchadmin，前面有一个search，两者不一样
    @PostMapping("/searchadmin/Assessment")
    public ModelAndView searchadmin(@RequestParam("assessmentName") String assessmentName) {
            Assessment assessment = assessmentService.getAssessmentByName(assessmentName);
            ModelAndView mav = new ModelAndView("/managehtml/assessmentManage.html");
            mav.addObject("results", assessment);
            return mav;
    }


    @RequestMapping("/delete/Assessment")
    public ModelAndView deleteAssessment(@RequestParam("assessmentName") String assessmentName) {
        // Delete assessment logic here
        if (assessmentName.isEmpty()){
            System.out.println("delete输入为空");
        }
        if(assessmentService.removeById(assessmentName)){
            System.out.println("删除成功");
        };

        ModelAndView mav = new ModelAndView("redirect:/manage_assessments");
        return mav;
    }


//    这里点击更新后，会跳转到xxxUpdata.html文件里面去，然后再执行更新
    @RequestMapping("/updatehtml/Assessment")
    public ModelAndView updateAssessmenthtml() {
        return new ModelAndView("redirect:/updatahtml/assessmentUpdata.html");
    }

    @RequestMapping("/update/Assessment")
    public ModelAndView updateAssessment(@RequestParam("assessmentName") String assessmentName,
                                         @RequestParam("assessmentResult") String assessmentResult,
                                         @RequestParam("suggestion") String suggestion,
                                         @RequestParam("status") String status) {
        // Update assessment logic here
//    这里逻辑比较复杂，为了方便，这里使用的是先删除对应数据，然后插入该数据的操作
        if(assessmentService.removeById(assessmentName)){
            System.out.println("更新——删除成功");
        };

        Assessment assessment = new Assessment();
        assessment.setAssessmentName(assessmentName);
        assessment.setAssessmentResult(assessmentResult);
        assessment.setSuggestion(suggestion);
        assessment.setStatus(status);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String currentDateTime = LocalDateTime.now().format(formatter);
        assessment.setCreationTime(currentDateTime);


        if(assessmentService.save(assessment)){
            System.out.println("更新——添加成功");
        };

        return new ModelAndView("redirect:/manage_assessments");
    }



    @RequestMapping("/add/Assessment")
    public ModelAndView addAssessment(@RequestParam("assessmentName") String assessmentName,
                                      @RequestParam("assessmentResult") String assessmentResult,
                                      @RequestParam("suggestion") String suggestion,
                                      @RequestParam("status") String status) {
        // Add assessment logic here
        Assessment assessment = new Assessment();
        assessment.setAssessmentName(assessmentName);
        assessment.setAssessmentResult(assessmentResult);
        assessment.setSuggestion(suggestion);
        assessment.setStatus(status);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String currentDateTime = LocalDateTime.now().format(formatter);
        assessment.setCreationTime(currentDateTime);


        if(assessmentService.save(assessment)){
            System.out.println("添加成功");
        };
        return new ModelAndView("redirect:/manage_assessments");
    }

}
